package com.schoolinfo.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.schoolinfo.dao.FirstClassDAO;
import com.schoolinfo.dao.SecondClassDAO;
import com.schoolinfo.model.SecondClass;


/**
 * 处理商品分类，
 * 负责主页分类显示等
 * @author wangjiawen
 *
 */
public class ClassAction extends ActionSupport implements Preparable{
	FirstClassDAO firstClassDAO;
	SecondClassDAO secondClassDAO;
	private int secondClassId;
	private int firstClassId;
	

	/**
	 * 保证第一次进入inde.jsp时,firstClassList不为空
	 */
	public void prepare() throws Exception
	{
		showClass();//初始化firstClassList，否则标签使用list时报错
	}
	
	public int getFirstClassId() {
		return firstClassId;
	}
	public void setFirstClassId(int firstClassId) {
		this.firstClassId = firstClassId;
	}
	public int getSecondClassId() {
		return secondClassId;
	}
	public void setSecondClassId(int secondClassId) {
		this.secondClassId = secondClassId;
	}
	public FirstClassDAO getFirstClassDAO() {
		return firstClassDAO;
	}
	public void setFirstClassDAO(FirstClassDAO firstClassDAO) {
		this.firstClassDAO = firstClassDAO;
	}
	public SecondClassDAO getSecondClassDAO() {
		return secondClassDAO;
	}
	public void setSecondClassDAO(SecondClassDAO secondClassDAO) {
		this.secondClassDAO = secondClassDAO;
	}
	
	/**
	 * 显示所有一级分类,
	 * 在request中储存firstClassList
	 * @return
	 */
	public String showClass()
	{
		List firstClassList=(List) ServletActionContext.getContext().getSession().get("menu");
		if(firstClassList==null || firstClassList.isEmpty()){
			firstClassList = firstClassDAO.findAll();
		//	ServletActionContext.getRequest().setAttribute("firstClassList", firstClassList);
			ServletActionContext.getContext().getSession().put("menu", firstClassList);
		}
		ServletActionContext.getRequest().setAttribute("firstClassList", firstClassList);
		return Action.NONE;
		//return "showClassSuccess";
	}
	
	/**
	 *  根据一级分类id显示二级分类
	 *  直接返回json字符串，用于ajax调用
	 * @return
	 * @throws IOException
	 */
	public String showSecondListByFirstId() throws IOException
	{
		List secondList  = secondClassDAO.findByFirstId( firstClassId);
		JSONArray array = new JSONArray();
		array.addAll(secondList);
	/*	SecondClass sc;
		String[] properties;
		StringBuffer sb = new StringBuffer();
		sb.append("[");
		for(int i=0;i<secondList.size();i++){
			properties=(String[]) secondList.get(i);
			sb.append("{'id':"+properties[0]);
			sb.append(",'name':"+properties[2]+"}");
			if(i!=secondList.size()-1)
				sb.append(",");
		}
		sb.append("]");
		
	*/
		String result = array.toString();
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		response.getWriter().write(result);
		return Action.NONE;
	}
	
	/**
	 * 获得一级分类json字符串
	 * @return
	 * @throws IOException
	 */
	public String showFirst() throws IOException
	{
		List firstClassList = firstClassDAO.findAll();
	
		JSONArray array = new JSONArray();
		array.addAll(firstClassList);
		//array.addAll(firstClassList);
		String result = array.toString();
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.println(result);
		out.flush();
		out.close();
		return NONE;
		//return ERROR;//
	}
	
	
}
